from django.db import models
from django.contrib.auth.models import User
# Create your models here.
import usermanage
from usermanage.models import UserInfo


class message(models.Model):
    # 发送者
    sender = models.ForeignKey(UserInfo, related_name='sent_message', on_delete=models.SET_NULL, blank=True, null=True,
                               verbose_name='发送者')
    # 接收者
    recipient = models.ForeignKey(UserInfo, related_name='receive_message', on_delete=models.SET_NULL, blank=True,
                                  null=True,
                                  verbose_name='接收者')
    # 发送内容
    messages = models.TextField(blank=True, null=True, verbose_name='发送内容')
    # 是否未读
    unread = models.BooleanField(default=True, db_index=True, verbose_name='是否未读')
    # 发送时间
    send_at = models.DateTimeField(auto_now_add=True, verbose_name='发送时间')
    # 能被发送者查看
    sender_can_see = models.BooleanField(default=True, null=True, verbose_name='发送者查看')
    # 能被接收者查看
    recipient_can_see = models.BooleanField(default=True, null=True, verbose_name='接收者查看')

    # 标记已读
    def mark_as_read(self):
        if self.unread:
            self.unread = False
            self.save()


class refuse_message(models.Model):
    # 拒收者
    refuser = models.ForeignKey(UserInfo, related_name='message_refuser', on_delete=models.SET_NULL, blank=True,
                                null=True,
                                verbose_name='拒收者')

    # 被拒收者
    refused = models.ForeignKey(UserInfo, related_name='message_refused', on_delete=models.SET_NULL, blank=True,
                                null=True,
                                verbose_name='被拒收者')
